package com.et.machine.manager.vm.version

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.Navigation
import com.et.machine.R
import com.et.machine.convert.machineInfo
import com.et.machine.databinding.FragmentVersionBinding
import com.et.machine.util.Version
import com.et.util.shell
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class VersionFragment : Fragment() {


    private lateinit var viewModel: VersionViewModel
    private lateinit var binding: FragmentVersionBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View = FragmentVersionBinding.inflate(inflater, container, false).apply {
        binding = this
    }.root

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        viewModel = ViewModelProvider(this).get(VersionViewModel::class.java)

        initBar()
        showVersion()


    }

    @SuppressLint("SetTextI18n")
    private fun initBar() {
        // back
        binding.layoutTitle.back.setOnClickListener {
            Navigation.findNavController(it).popBackStack()
        }

        binding.layoutTitle.uuid.text =
            "机器编号：${machineInfo?.uuid}"

        binding.layoutTitle.title.setText(R.string.version_label)
    }

    @SuppressLint("SetTextI18n")
    private fun showVersion() {
        val string = StringBuilder(512)
        shell("pm list packages", string, false)
        var packageName: String? = null
        string.split('&').apply {
            if (isNotEmpty()) {
                forEach {
                    when {
                        it.contains("com.et.u0") -> {
                            packageName = "com.et.u0"
                            return@forEach
                        }
                        it.contains("com.et.u1") -> {
                            packageName = "com.et.u1"
                            return@forEach
                        }
                        it.contains("com.et.u2") -> {
                            packageName = "com.et.u2"
                            return@forEach
                        }
                        it.contains("com.et.u3") -> {
                            packageName = "com.et.u3"
                            return@forEach
                        }
                    }
                }
            }
        }

        CoroutineScope(Dispatchers.Default).launch {
            val launchVersion = Version.packageVersion(requireContext(), "com.et.launch")
            val machineVersion = Version.packageVersion(requireContext(), "com.et.machine")
            val uVersion = Version.packageVersion(requireContext(), packageName!!)
            val install = Version.packageVersion(requireContext(), "com.et.install")


            CoroutineScope(Dispatchers.Main).launch {
                binding.launchVersion.text = "启动器版本:$launchVersion"
                binding.machineVersion.text = "硬件驱动版本：$machineVersion"
                binding.utilVersion.text = "升级器版本：$install"
                binding.uVersion.text = "售卖版本：$uVersion"

            }
        }
    }
}